﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using Tools;
using Wp.DAL;

namespace Wp.BLL
{
    /// <summary>
    /// Summary description for TblPostMeta
    /// </summary>
    public class TblPostMetaManagerBase
    {
        public TblPostMetaManagerBase()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public void InsertUpdate(TblPostMetaEntity item)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", item.Id));
            ls.Add(new SqlParameter("@PostId", item.PostId));
            ls.Add(new SqlParameter("@MetaKey", item.MetaKey));
            ls.Add(new SqlParameter("@MetaValue", item.MetaValue));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_InsertUpdateTblPostMeta", sqlparam);
        }
        public void InsertUpdate(int Id, int PostId, string MetaKey, string MetaValue)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            ls.Add(new SqlParameter("@PostId", PostId));
            ls.Add(new SqlParameter("@MetaKey", MetaKey));
            ls.Add(new SqlParameter("@MetaValue", MetaValue));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_InsertUpdateTblPostMeta", sqlparam);
        }
        public void Delete(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_DeleteTblPostMeta", sqlparam);
        }
        public TblPostMetaEntity GetTblPostMetaEntity(DataRow dr)
        {
            TblPostMetaEntity item = new TblPostMetaEntity();            
            if (dr.Table.Columns.Contains("Id"))
                item.Id = dr["Id"].ToInt();              
            if (dr.Table.Columns.Contains("PostId"))
                item.PostId = dr["PostId"].ToInt();              
            if (dr.Table.Columns.Contains("MetaKey"))
                item.MetaKey = dr["MetaKey"].ToString();              
            if (dr.Table.Columns.Contains("MetaValue"))
                item.MetaValue = dr["MetaValue"].ToString();              
            return item;
        }        
        public DataTable SelectOneRDT(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            return SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPostMeta", sqlparam);
        }
        public TblPostMetaEntity SelectOne(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPostMeta", sqlparam);
            if(dt!=null && dt.Rows.Count>0)
                return GetTblPostMetaEntity(dt.Rows[0]);
            return null;
        }
        public TblPostMetaEntity SelectOne(int PostId,string MetaKey)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@PostId", PostId));
            ls.Add(new SqlParameter("@MetaKey", MetaKey));
            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPostMeta", sqlparam);
            if (dt != null && dt.Rows.Count > 0)
                return GetTblPostMetaEntity(dt.Rows[0]);
            return null;
        }
        public DataTable SelectAllRDT()
        {
            return SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPostMetasAll", null);
        }
        public List<TblPostMetaEntity> SelectAll()
        {
            List<TblPostMetaEntity> lsresult= new List<TblPostMetaEntity>();
            DataTable dt =  SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPostMetasAll", null);
            if (dt != null && dt.Rows.Count > 0)
                for (int i = 0; i < dt.Rows.Count; i++)
                    lsresult.Add(GetTblPostMetaEntity(dt.Rows[i]));            
            return lsresult;
        }
    }
}

